﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HighLowBase;
using HighLowTriple7;
using PaymentBLL;

public partial class Pages_Demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int numUser = Convert.ToInt32(txtUserCount.Text);
        int minVnd = Convert.ToInt32(txtMinVnd.Text);
        int maxVnd = Convert.ToInt32(txtMaxVnd.Text);
        int maxRound = Convert.ToInt32(txtRoundMax.Text);
        ResetUser(numUser, minVnd, maxVnd, maxRound);
    }
    Random ran = new Random();
    private void ResetUser(int total, int minVnd, int maxAmountVnd, int maxLevel)
    {
        var ctr = new CtrAccount();
        ctr.ClearAccount();
        var ctrG = new CtrPaymentGateway();
        var gates = ctrG.SelectAllPaymentGateway();
        for (int i = 0; i < total; i++)
        {
            var idx = ("00000" + i);
            idx = idx.Substring(idx.Length - 4);
            //var ran=new Random();
            var t = ran.Next(1000000, 7000000);
            t = t / 1000000;
            int? id = 0;
            ctr.InsertAccount("test_" + idx, "123456", t, "test_" + idx, "test_" + idx + "@email.com", 1, ref id);

            //var ran1 = new Random();
            var g = ran.Next(0, gates.Count);

            //var ran2 = new Random();
            int amountVnd = ran.Next(minVnd * 1000, (maxAmountVnd * 1000) + 1);

            amountVnd = amountVnd / 100000;
            amountVnd = amountVnd * 100;

            var au = (Int32)(amountVnd * gates[g].ReceiveAu / gates[g].VndIn);
            var ag = (Int32)(amountVnd * gates[g].ReceiveAg / gates[g].VndIn);

            var amount = (Int32)(amountVnd * gates[g].AmountIn / gates[g].VndIn);
            ctr.AddAccountAuAgIn(id ?? 0, "test_" + idx, 1, gates[g].Id, gates[g].Name, gates[g].Unit,
                "Mã user: " + idx, "order:1|01928273583|Mua hàng",
                amount, amountVnd, au, ag, 1, 1, "Nạp tiền qua " + gates[g].Name);
        }
    }

    private int GetRandomBet(int current, int minBet, int maxBet)
    {
        int bet = maxBet - minBet;
        bet = ran.Next(1, bet * 1000 + 1) % bet + 1 + minBet;
        if (bet > current)
            bet = current;
        bet = bet / 100 * 100;

        if (bet > maxBet)
            bet = maxBet;
        if (bet < minBet)
            return 0;
        return bet;
    }

    private void PlayGame(PlayParam p)
    {
        try
        {
            var game = HiLoGame.LoadGameInfo(1);
            var min = (int)game.MinimumBet;

            var max = (int)game.MaximumBet;
            int accId = p.accId;
            int au = p.au;
            int ag = p.ag;
            string accName = p.accName;
            int accType = p.accType;
            int maxLevel = p.maxLevel;

            var ctrAccount = new CtrAccount();
            var accessToken = Triple7.Instance(accId, accName, accType);
            int level = 0;
            int bet = 0;

            level = 0;
            int col = 1;
            int cur = 0;
            int status;
            while (level <= maxLevel)
            {
                Thread.Sleep(1000);

                level++;
                status = Triple7.GetGameStatus(accessToken);
                if (status == 4)
                {
                    if (ran.Next(100000000, 300000000) % 2 == 1)
                    {
                        bet = GetRandomBet(au, min, max);
                        if (bet == 0)
                            break;
                        Triple7.PlayerBetAu(bet, accessToken);
                    }
                    else
                    {
                        bet = GetRandomBet(ag, min, max);
                        if (bet == 0)
                            break;
                        Triple7.PlayerBetAg(bet, accessToken);
                    }
                }

                if (status == 5)
                {
                    if (Triple7.GetRound(accessToken) > 1)
                    {
                        if (ran.Next(1, 1000000) % 2 == 0)
                        {
                            Triple7.PlayerCollect(accessToken);
                            break;
                        }
                    }
                    var val = ran.Next(1, 39000000) % 39 + 1;

                    bool? hi = null;

                    if (val <= 13)                              //Cột 1
                    {
                        col = 1;
                        cur = Triple7.GetColumn1Value(accessToken);
                        if (val >= 1 && val < cur)
                            hi = true;
                        else if (val > cur && val <= 13)
                            hi = false;
                        else
                        {
                            if (cur > 7)
                            {
                                hi = true; val--;
                            }
                            else
                            {
                                hi = false; val++;
                            }
                        }

                    }
                    else if (val > 13 && val <= 26)             //Cột 2
                    {
                        col = 2;
                        cur = Triple7.GetColumn2Value(accessToken);

                        if (val > 13 && val < cur)
                            hi = true;
                        else if (val > cur && val <= 26)
                            hi = false;
                        else
                        {
                            if (cur > 20)
                            {
                                hi = true; val--;
                            }
                            else
                            {
                                hi = false; val++;
                            }
                        }
                    }
                    else if (val > 26 && val <= 39)             //Cột 3
                    {
                        col = 3;
                        cur = Triple7.GetColumn3Value(accessToken);

                        if (val > 26 && val < cur)
                            hi = true;
                        if (val > cur && val <= 39)
                            hi = false;
                        else
                        {
                            if (cur > 33)
                            {
                                hi = true; val--;
                            }
                            else
                            {
                                hi = false; val++;
                            }
                        }
                    }

                    if (hi.Value)
                        Triple7.PlayerSelectHigh(col, accessToken);
                    else
                        Triple7.PlayerSelectLow(col, accessToken);
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnPayNext_Click(object sender, EventArgs e)
    {
        int maxRound = Convert.ToInt32(txtRoundMax.Text);
        var ctr = new CtrAccount();
        int? totalRecord = 0;
        var lst = ctr.SelectAccountBy(-1, -1, "", 1, 1, Int32.MaxValue, ref totalRecord);
        foreach (var account in lst)
        {
            var inf = ctr.SelectAccountInfo(account.Id);

            var p = new PlayParam()
            {
                accId = account.Id,
                au = (Int32)inf.Au,
                ag = (Int32)inf.Ag,
                accName = account.Name,
                accType = account.TypeId,
                maxLevel = maxRound
            };

            new Thread(() =>
               PlayGame(p)
           ).Start();
            Thread.Sleep(10);
        }
    }
}
public class PlayParam
{
    public int accId { get; set; }
    public int au { get; set; }
    public int ag { get; set; }
    public string accName { get; set; }
    public int accType { get; set; }
    public int maxLevel { get; set; }
}